import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
void main() => runApp(MyApp());

ValueNotifier<int> _valueNotifier = ValueNotifier<int>(0);//数据监听器

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Material App Bar'),
        ),
        body: Center(
          child: Container(
            child: Column(
              children: [
                ChildWidget1(),
                ChildWidget2(),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class ChildWidget1 extends StatefulWidget {
  @override
  _ChildWidget1State createState() => _ChildWidget1State();
}

class _ChildWidget1State extends State<ChildWidget1> {
  @override
  Widget build(BuildContext context) {
    print('YM---ChildWidget1----进行刷新啊');
    return ValueListenableBuilder<int>(
      valueListenable: _valueNotifier,
      builder: (context,value,child){
        print('YM----ChildWidget1-----ValueListenableBuilder----进行刷新啊');
        return Text('ChildWidget1新值:$value');
      },
      // child: Container(
      //   child: Text('子控件'),
      // ),
    );
  }
}

class ChildWidget2 extends StatefulWidget {
  @override
  _ChildWidget2State createState() => _ChildWidget2State();
}

class _ChildWidget2State extends State<ChildWidget2> {
  delayed() async {
    while(true){
      await Future.delayed(new Duration(seconds: 2));
      _valueNotifier.value++;
    }
  }
  @override
  Widget build(BuildContext context) {
    print('YM---ChildWidget2----进行刷新啊');
    return Container(
      child: RaisedButton(
        onPressed: (){
          delayed();
        },
        child: Text('点击更改其它地方的数据'),
      ),
    );
  }
}

// class ListWidget extends StatefulWidget{
//   @override
//   _ListWidget createState() => _ListWidget();
// }
//
// class _ListWidget extends State<ListWidget>{
//   List<Widget> list = [];
//   _getList(){
//     for(var i=0;100>i;i++){
//       list.add(ListTile(
//         title: Text('$i'),
//       ));
//     }
//     return list;
//   }
//   @override
//   Widget build(BuildContext context) {
//     return ListView(
//       children: _getList(),
//     );
//   }
// }